package cn.yangyumanage.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.yangyumanage.R;
import cn.yangyumanage.bean.DouBanSearchItemBean;
import cn.yangyumanage.fragment.FilmAddFragment;

public class DouBanSearchRvAdapter extends RecyclerView.Adapter<DouBanSearchRvAdapter.MyViewHolder> {
    private Context context;
    private ViewPager2 viewPager2;
    private List<DouBanSearchItemBean> douBanSearchItemBeans;
    private FilmAddFragment filmAddFragment;
    private final Bundle bundle = new Bundle();

    public DouBanSearchRvAdapter(Activity activity, List<DouBanSearchItemBean> douBanSearchItemBeans, ViewPager2 viewPager2, FilmAddFragment filmAddFragment) {
        this.context = activity;
        this.douBanSearchItemBeans = douBanSearchItemBeans;
        this.viewPager2 = viewPager2;
        this.filmAddFragment = filmAddFragment;
    }

    @NonNull
    @Override
    public DouBanSearchRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(this.context).inflate(R.layout.douban_search_rv_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DouBanSearchRvAdapter.MyViewHolder holder, int position) {
        Glide.with(context).load(douBanSearchItemBeans.get(position).getPostUrl()).into(holder.iv_post);
        holder.tv_name.setText(douBanSearchItemBeans.get(position).getName());
        holder.tv_primitive_name.setText(douBanSearchItemBeans.get(position).getPrimitiveName());
        holder.tv_plot.setText(douBanSearchItemBeans.get(position).getPlot());
        holder.ll_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bundle.clear();
                bundle.putString("sid", douBanSearchItemBeans.get(holder.getAdapterPosition()).getSid());
                filmAddFragment.setArguments(bundle);
                viewPager2.setCurrentItem(1);
            }
        });
    }

    @Override
    public int getItemCount() {
        return this.douBanSearchItemBeans.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ImageView iv_post;
        private final TextView tv_name;
        private final TextView tv_primitive_name;
        private final TextView tv_plot;
        private final LinearLayout ll_search;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_post = itemView.findViewById(R.id.iv_post);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_primitive_name = itemView.findViewById(R.id.tv_primitive_name);
            tv_plot = itemView.findViewById(R.id.tv_plot);
            ll_search = itemView.findViewById(R.id.ll_search);
        }
    }
}
